//
//  LCForgetPasswordVC.m
//  LongChain
//
//  Created by FengZi on 2018/4/8.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCForgetPasswordVC.h"
#import "LCUserModel.h"
@interface LCForgetPasswordVC ()

@property (weak, nonatomic) IBOutlet UITextField *usePhoneTextField;

@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@end

@implementation LCForgetPasswordVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self customTranslucentNavigationBar];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *bgImage = [[UIImageView alloc]initWithFrame:self.view.frame];
    bgImage.image = [UIImage imageNamed:@"publicBackground"];
    [self.view addSubview:bgImage];
    [self.view insertSubview:bgImage atIndex:0];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldChnage) name:UITextFieldTextDidChangeNotification object:nil];
    
}

- (void)textFieldChnage {
    if (self.usePhoneTextField.text.length >= 11) {
        
        NSString *currentPhone = [self.usePhoneTextField.text substringWithRange:NSMakeRange(0, 11)];
        self.usePhoneTextField.text = currentPhone;
        self.nextButton.userInteractionEnabled = YES;
        self.nextButton.backgroundColor = [UIColor whiteColor];
        [self.nextButton setTitleColor:RGB(80.0f, 149.0f, 244.0f) forState:UIControlStateNormal];

        [self.usePhoneTextField resignFirstResponder];
    }else{
        self.nextButton.userInteractionEnabled = NO;
        self.nextButton.backgroundColor = RGBAlpha(255, 255, 255, 0.5);
        [self.nextButton setTitleColor:RGBAlpha(255.0f, 255.0f, 255.0f, 0.6) forState:UIControlStateNormal];
    }
}
- (IBAction)forgetPasswordNext:(id)sender {
    
    NSDictionary *sendRegistCode = @{@"phone":self.usePhoneTextField.text};
    [LCUserModel userModelManager].mobile = self.usePhoneTextField.text;
    

    [[LCNetwork shareInstance]lc_postNetworkWithRUL:@"/api/v0/sms" parameter:sendRegistCode responseSuccess:^(id resultCode, id resultParameter) {
        
        
        LCLog(@"resultCode = %@ resultParameter = %@",resultCode,resultParameter);
        NSString *code = [NSString stringWithFormat:@"%@",resultParameter[@"sms_code"]];
        self.registCode = code;
        NSString *token = [NSString stringWithFormat:@"%@",resultParameter[@"sms_token"]];
        
         [LCUserModel userModelManager].userid = token;
        [self performSegueWithIdentifier:@"showRegistCode" sender:self];

        
    } responseFail:^(id resultCode, NSError *error) {
        
        [self showHint:@"网络异常,请稍后再试"];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"showRegistCode"]) {
        id vc = segue.destinationViewController;
        
        [vc setValue:self.usePhoneTextField.text forKey:@"phoneNum"];
        [vc setValue:self.registCode forKey:@"registCode"];
        [vc setValue:@"1" forKey:@"type"];

        
    }
}


@end
